<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\model\BaseSupplier as Model;
use think\facade\Db;
use think\Request;

class OilSupplier extends Controller
{
    /**
     * @api {GET} /admin/oilSupplier/ 供应商列表
     * @apiName 供应商列表
     * @apiGroup oilAdmin
     * @apiParam {String} [name] 供应商名称
     * @apiParam {String} [code] 别名
     * @apiUse PageReturn
     * @apiVersion 1.0.0
     */
    public function index(Request $request)
    {
        $where = [];
        if ($name = $request->param('name', '')) {
            $where[] = ['name', 'like', '%'.$name.'%'];
        }
        if ($code = $request->param('code', '')) {
            $where[] = ['code', 'like', '%'.$code.'%'];
        }
        $paginate = $request->only(['page' => 1, 'list_rows' => 15]);
        if ($where) {
            $list = Model::where($where)->field(Db::raw('*,api_config as config'))->paginate($paginate);
        } else {
            $list = Model::paginate($paginate);
        }

        if ($list) {
            return success('', $list->toArray());
        } else {
            return fail();
        }
    }

    /**
     * @api {POST} /admin/oilSupplier/save 新建或者保存
     * @apiName 供应商保存
     * @apiGroup oilAdmin
     * @apiParam {Number} [id] id 更新时候传递
     * @apiParam {String} name 名称
     * @apiParam {String} code 别名
     * @apiParam {String} [config[key]] 配置
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function save(Request $request)
    {
        $rules = [
            'name' => 'require',
            'code' => 'require',
            //'config' => 'require',
        ];
        $messages = [
            'name.require' => '请输入名称',
            'code.require' => '请输入代号',
            //'config.require' => '请',
        ];
        $param = $request->only(['name', 'code', 'config' => '']);
        $id = $request->param('id', 0);
        if ($msg = dmjValidate($rules, $messages, $param)) {
            return fail($msg);
        }
        if (mb_strlen(json_encode($param['config'])) > 255) {
            return fail('配置项多，请不要配置无用东西，或者联系技术');
        }

        $param['api_config'] = $param['config'];

        if ($id) {
            $existData = Model::where('id', '<>', $id)->where(function ($query) use ($param) {
                $query->where('name', '=', $param['name'])->whereOr('code', '=', $param['code']);
            })->find();
        } else {
            $existData = Model::where('name', '=', $param['name'])->whereOr('code', '=', $param['code'])->find();
        }

        // 名称校验
        if ($existData) {
            return fail('名称或者代号和id='.$existData['id'].'的冲突了');
        }
        if ($id) {
            $oilAgentChannel = Model::find($id);
        } else {
            $oilAgentChannel = new Model();
        }

        if ($oilAgentChannel->save($param)) {
            return success();
        } else {
            return fail();
        }
    }

    /**
     * @api {GET} /admin/oilSupplier/read 供应商详情
     * @apiName 供应商详情
     * @apiGroup oilAdmin
     * @apiParam {Number} id id
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function read(Request $request)
    {
        if (!$id = $request->param('id', 0)) {
            return fail('缺少必要参数');
        }

        $data = Model::find($id);

        if ($data) {
            return success('', $data->toArray());
        } else {
            return fail('数据不存在');
        }
    }

    /**
     * @api {POST} /admin/oilSupplier/delete 供应商删除
     * @apiName 供应商删除
     * @apiGroup oilAdmin
     * @apiParam {Number} id id
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function delete(Request $request)
    {
        if (!$id = $request->param('id', 0)) {
            return fail('缺少必要参数');
        }
        if (Model::destroy($id)) {
            return success();
        }

        return fail();
    }
}
